package com.cz;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import com.cz.utils.DBOpenHelper;
import com.cz.utils.UserManage;
import java.util.HashMap;

public class LoginActivity extends AppCompatActivity {

    private Button but;
    private EditText usernameText;
    private EditText passwordText;
    //获取数据库工具
    private DBOpenHelper dbOpenHelper;
    private  HashMap<String, String> map;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        dbOpenHelper = new DBOpenHelper(LoginActivity.this,"WeChatMini",null,2);

        //获取登录按钮并监听登录按钮点击事件
        but = findViewById(R.id.login_but);
        but.setOnClickListener(new View.OnClickListener() {

            //点击事件
            @Override
            public void onClick(View v) {
                usernameText = findViewById(R.id.username);
                passwordText = findViewById(R.id.password);

                /*登录验证*/
                Boolean flag = Login();

                if (flag){
                    Intent intent = new Intent();

                    //下一个Activity按返回键不返回到此活动
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setClass(LoginActivity.this,MainActivity.class);
                    startActivity(intent);

                }
            }
        });
    }




    /*
    * 登录验证
    * 1.健壮性判断
    * 2.数据库查询
    * */
    private Boolean Login() {
        //获取用户名
        String username = usernameText.getText().toString();
        //获取密码
        String password = passwordText.getText().toString();

        //1.非空验证
        Boolean empty = isEmpty(username, password);

        if(!empty){
            //数据库查询验证
            return selectDataBase(username, password);
        }
        return false;
    }
    /*
    * 数据库查询
    * 1. 先查用户名是否存在(数据库设计用户名为主键,不考虑重复)
    * 2. 然后差密码是否匹配
    * */
    private Boolean selectDataBase(String username,String password) {

        SQLiteDatabase db= dbOpenHelper.getReadableDatabase();

        insertInfo0(db);
        //更具userId查询数据

        Cursor user = db.query("mine", null, "userid=?",
                new String[]{username}, null, null, null);

        map = new HashMap<>();

        while(user.moveToNext()){
            map.put("userId",user.getString(0));
            map.put("password",user.getString(1));
            map.put("nickname",user.getString(2));
            map.put("phone",user.getString(3));
            map.put("headimg",user.getString(4));
            map.put("address",user.getString(5));
            Log.i("userId",user.getString(0));
            Log.i("password",user.getString(1));

        }
        if(map.get("userId")==null){
            usernameText.setText("账号不存在!");
            usernameText.setHintTextColor(ContextCompat.getColor(LoginActivity.this, R.color.red));
        }

        //密码验证
        if(!password.equals(map.get("password"))){
            passwordText.setText("");
            passwordText.setHint("密码有误！请输入正确的密码");
            passwordText.setHintTextColor(ContextCompat.getColor(LoginActivity.this, R.color.red));
        }else{
            System.out.println("登录成功" + map.get("password"));
            UserManage.getInstance().saveUserInfo(LoginActivity.this, map.get("nickname"), map.get("password"));
            return true;
        }

        dbOpenHelper.close();
        return false;
    }

    /* 非空验证 */
    private Boolean isEmpty(String username, String password) {
        if(username.isEmpty()||password.isEmpty()){
            if(username.isEmpty()){
                usernameText.setHint("请输入QQ/邮箱/手机号");
                usernameText.setHintTextColor(ContextCompat.getColor(LoginActivity.this, R.color.red));
            }
            if (password.isEmpty()){
                passwordText.setHint("请输入密码");
                passwordText.setHintTextColor(ContextCompat.getColor(LoginActivity.this, R.color.red));
            }
            return true;
        }else{
            return false;
        }
    }

    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo0(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("userid","cz");
        contentValues.put("password","000000");
        contentValues.put("nickname","陈卓");
        contentValues.put("phone","15310173757");
        contentValues.put("headimg","icon");
        contentValues.put("address","贵州盘县");
        long mine = db.insert("mine",null, contentValues);
    }

}
